#include "../include/data_global.h"

extern int dev_buzzer_fd;
extern char dev_buzzer_mask;

extern pthread_mutex_t mutex_buzzer;

extern pthread_cond_t cond_buzzer;

void *pthread_buzzer (void *arg)
{
	if ((dev_buzzer_fd = open (DEV_BUZZER, O_RDWR | O_NONBLOCK)) < 0)
	{
        perror("can't open file /dev/beep");
		exit (-1);
	}
    puts("pthread_buzzer is ok");

	while (1)
	{
		pthread_mutex_lock (&mutex_buzzer); //加锁
		pthread_cond_wait (&cond_buzzer, &mutex_buzzer);//等待客户端请求线程发送信号
		printf ("pthread_buzzer is wake up\n");
		if (dev_buzzer_mask)
        {
			ioctl(dev_buzzer_fd, PWM_ON);
            printf("**buzzer on\n");
		}else
        {
			ioctl(dev_buzzer_fd, PWM_OFF);
            printf("**buzzer off\n");
		}
		pthread_mutex_unlock (&mutex_buzzer);//释放锁
	}

    /*test*/
    /*
    while(1)
    {

        ioctl(dev_buzzer_fd, PWM_ON);
        sleep(1);
        ioctl(dev_buzzer_fd, PWM_OFF);
        sleep(1);
    }
    */
    /*test*/

	return 0;
}

